package com.wanghuadi.controller;

import com.wanghuadi.beans.*;
import com.wanghuadi.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping("list")
    public String list(Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Student> list = studentService.findAll(map);
        model.addAttribute("list",list);
        return "list";
    }

    /**
     * 修改回显
     * @param id  学生id
     * @return
     */
    @RequestMapping("findByStudentById")
    @ResponseBody
    public Map<String, Object> findByStudentById(Integer id){

        Map<String,Object> map = new HashMap<String, Object>();

        //1. 学生信息回显
        Student student = studentService.findByStudentById(id);
        map.put("student",student);

        //2. 动态加分类型下拉
        List<Type> typeList = studentService.queryType();
        map.put("typeList",typeList);

        //3. 性别
        Gender[] genders = Gender.values();
        map.put("genders",genders);

        //4. 分类
        Category[] categories = Category.values();
        map.put("categories",categories);

        //5. 学科
        Subject[] subjects = Subject.values();
        map.put("subjects",subjects);

        return map;
    }


    @RequestMapping("updateStudent")
    @ResponseBody
    public boolean updateStudent(Student student){
        try {
            studentService.updateStudent(student);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
